package com.crl.rcdp.sys.dao;

import com.cars.util.page.PageUtil;
import com.crl.dao.BaseDao;
import com.crl.rcdp.sys.model.SysUser;
import com.crl.rcdp.sys.vo.SysUserSearchVO;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/22 11:10
 */
@Repository
public class SysUserDao2 extends BaseDao<SysUser, SysUserSearchVO> {

    /**
     * 查询全部，用于选择角色
     */
    public List<SysUser> list(SysUserSearchVO sysUserSearchVO) {
        String sql = "SELECT * FROM td_sys_user where 1=1 ";
        sql += createSQL(sysUserSearchVO);
        sql += " order by created_at";
        sql = PageUtil.createOraclePageSQL(sql, sysUserSearchVO.getPage());
        return list(sql, sysUserSearchVO);
    }

    /**
     * 增加角色
     */
    public int add(SysUser sysUser) {
        String sql = "INSERT INTO td_sys_user (id,username,real_name,telephone,mobile,idcard,created_by,created_at)" +
                " VALUES(seq_td_sys_user.nextval,:username,:realName,:telephone,:mobile,:idcard,:createdBy,sysdate)";
        return insertForId(sql, sysUser, "id");
    }

    /**
     * 根据角色id删除
     */
    public int delete(Integer id) {
        String sql = "DELETE FROM td_sys_user WHERE id=?";
        return update(sql, id);
    }

    /**
     * 根据角色id修改
     */
    public int update(SysUser sysUser) {
        String sql = "UPDATE td_sys_user SET real_name=:realName,telephone=:telephone,mobile=:mobile,idcard=:idcard,modified_by=:modifiedBy,modified_at=:modifiedAt WHERE id=:id ";
        return update(sql, sysUser);
    }

    /**
     * 统计查询数据条数
     */
    public int count(SysUserSearchVO sysUserSearchVO) {
        String sql = "SELECT COUNT(id) FROM td_sys_user WHERE 1=1 ";
        sql += createSQL(sysUserSearchVO);
        return count(sql, sysUserSearchVO);
    }

    /**
     * 拼接查询条件
     */
    private String createSQL(SysUserSearchVO sysUserSearchVO) {
        String sql = "";
        return sql;
    }

    /**
     * 根据id查询
     */
    public SysUser get(Integer id) {
        String sql = "SELECT * FROM td_sys_user WHERE id=?";
        return get(sql, id);
    }
}
